﻿namespace Quail
{
    /// <summary>
    /// A web link created by the link text
    /// </summary>
    public class WebLinkByText : WebLink
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="WebLinkByText"/> class.
        /// </summary>
        /// <param name="container">The owning control or page.</param>
        /// <param name="linkText">The link text.</param>
        public WebLinkByText(WebControlContainer container, string linkText)
            : base(container, GetWebLinkByTextSelector(linkText))
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="WebLinkByText"/> class.
        /// </summary>
        /// <param name="container">The owning control or page.</param>
        /// <param name="linkText">The link text.</param>
        /// <param name="index">The index.</param>
        public WebLinkByText(WebControlContainer container, string linkText, int index)
            : base(container, GetWebLinkByTextSelector(linkText, index))
        {
        }

        /// <summary>
        /// Generates the WebLinkByText jQuery selector.
        /// </summary>
        /// <param name="linkText">The link text.</param>
        /// <returns>a jQuery selector</returns>
        public static string GetWebLinkByTextSelector(string linkText)
        {
            return GetWebLinkByTextSelector(linkText, 0);   
        }

        /// <summary>
        /// Generates the WebLinkByText jQuery selector.
        /// </summary>
        /// <param name="linkText">The link text.</param>
        /// <param name="index">The index.</param>
        /// <returns>a jQuery selector</returns>
        public static string GetWebLinkByTextSelector(string linkText, int index)
        {
            return index == 0
                       ? string.Format("a:linkbytext('{0}'):first", linkText)
                       : string.Format("a:linkbytext('{0}'):lazyindex({1})", linkText, index);
        }
    }
}
